﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sub.TaskPlanningDesk.Model.Objects;
using Project = Sub.TaskPlanningDesk.Model.Objects.Project;
using Sub.TaskPlanningDesk.Model.Infrastructure;

namespace Sub.TaskPlanningDesk.Model.Repositories
{
	public class ProjectRepository : IProjectRepository
	{
		TaskPlanningDBEntities Context;

		public ProjectRepository()
		{
			Context = new TaskPlanningDBEntities("TaskPlanningDBEntities");
		}

		public Project[] GetProjects(int userId)
		{
			var projectIDs = Context.ProjectToUsers.Where(r => r.UserID == userId)
				.Select(r => r.ProjectID);
			var projects = Context.Projects.Where(r => projectIDs.Contains(r.ID));

			return projects.Select(r => new Project(){
				ID = r.ID,
				Name = r.Name,
				Description =r.Description

			}).ToArray();
		}

		public Project ByID(int projectID)
		{
			var dbProject = Context.Projects.Single(r => r.ID == projectID);
			return new Project()
			{
				ID = dbProject.ID,
				Name = dbProject.Name,
				Description = dbProject.Description
			};
		}
	}
}
